'use strict';

/**
 * Created by GCX on 2018/1/29.
 */
$(function () {

    layui.use(['form', 'layer'], function () {

        var form = layui.form,
            layer = layui.layer;

        var reply_id = 0;
        var person = 0;
        var title = '';
        var current_floor = 0;
        var detail_id = 0;

        // $('.forum-content').on('click', '.close-textarea', function () {
        //     $(this).parents('.reply-info-box').css('display', 'none');
        // });
        // $('.forum-content').on('click', '.reply-comment', function () {
        //
        //     $(document).find('.reply-info-box').css('display', 'none');
        //
        //     $(this).parents('.forum-box').find('.reply-info-box').css('display', 'inline-block');
        //
        // });

        /**
         * 楼层回复按钮点击事件
         * 点击后生成textarea输入框
         */
        $('.floor').on('click', '.reply-btn', function () {
            var body = $(this).parents('.floor-flex').next();
            var target = body.find('.floor-content');
            // 如果已经存在textarea则不生成
            if (body.find('.reply-comment-container').length) return;
            reply_id = $(this).attr('data-reply-id');
            person = $(this).attr('data-user-id');
            title = $(this).attr('data-title');
            current_floor = $(this).attr('data-floor');
            detail_id = $(this).attr('data-detail-id');

            // 需要生成的textarea内容
            var reply_html = '<div class="reply-comment-container">' + '<div style="width: 100%;">' + '<textarea name="reply_comment" class="form-control reply-textarea" id="reply-textarea"' + 'style="max-width: 100%; max-height: 200px; min-height: 100px; resize: none; margin-bottom: 20px;"></textarea>' + '</div>' + '<button type="button" class="btn btn-primary reply-comment-btn">提交</button>' + '<button type="button" class="btn btn-default reply-comment-cancel">取消</button>' + '</div>';
            // 添加textarea控件之前移除现展开的楼层textarea控件
            $('.floor').each(function (index) {
                var el = $(this).find('.reply-comment-container');
                if (el.length) {
                    el.remove();
                }
            });
            target.after(reply_html);
            setTimeout(function () {
                $('#reply-textarea').focus();
            }, 50);
        });

        $('.floor').on('click', '.reply-comment-btn', function () {
            var content = $(this).prev().find('textarea[name="reply_comment"]').val();
            // 如果回复的id不为真或未能获取到则报错
            if (!reply_id || !person || !title) {
                layer.msg('参数错误', { icon: 2, time: 2000 });
                return;
            }
            if (!$.trim(content)) {
                layer.msg('回复内容不能为空！', { icon: 2, time: 2000 });
                return;
            }
            var reply_comment_load = layer.load(2, {
                shade: [0.5, '#fff'] //0.1透明度的白色背景
            });
            // 提交回复数据
            $.post('/Summary/comment', {
                content: content,
                person: person,
                reply_id: reply_id,
                title: title,
                current_floor: current_floor,
                detail_id: detail_id
            }, function (response) {
                if (response.flag) {
                    layer.msg(response.msg, { icon: 1, time: 1500 });
                    setTimeout(function () {
                        location.reload();
                    }, 1500);
                } else {
                    layer.alert(response.msg);
                    layer.closeAll();
                }
            }, 'json');
        });

        $('.floor').on('click', '.reply-comment-cancel', function () {
            $(this).parent().remove();
        });

        // 判断是否为空 空格 回车
        function javaTrim(str) {
            for (var i = 0; str.charAt(i) == ' ' && i < str.length; i++) {}
            if (i == str.length) return '';
            var newstr = str.substr(i);
            for (var i = newstr.length - 1; newstr.charAt(i) == ' ' && i >= 0; i--) {}
            newstr = newstr.substr(0, i + 1);
            return newstr;
        }

        $('#submit-discuss-content').click(function (evt) {

            // 判断是否输入的空格 回车 空
            var strValue = editor.txt.text();
            var string = strValue.replace(/\&nbsp\;/ig, ' ');

            if (!$.trim(string).length) {
                layer.msg('内容不能为空！', { icon: 2, time: 2000 });
                return false;
            }

            // 拼装提交数据
            var result = new Object();
            result.content = editor.txt.html();
            result.uid = $('#submit-discuss-content').attr('discuss_id');
            result.id = $('.discuss-submit-box .hiddenInput').val();
            result.create_id = $('.discuss-submit-box .hidden-user').val();
            result.title = $('.discuss-submit-box ._title').val();

            $.ajax({
                url: ThinkPHP['AJAX'] + '/Summary/reply',
                data: result,
                type: 'POST',
                dataType: 'json',
                success: function success(response) {
                    if (response.flag > 0) {
                        layer.msg(response.msg, { time: 2000, icon: 1 });
                    } else {
                        layer.msg(response.msg, { time: 2000, icon: 2 });
                    }
                    setTimeout(function () {
                        location.reload();
                    }, 2000);
                }
            });

            var index = layer.load(2, {
                shade: [0.5, '#fff'] //0.1透明度的白色背景
            });
        });

        $('.forum-content').on('click', '.submit-btn', function (evt) {

            var strValue = $(this).prev().prev().val();
            strValue = strValue.replace(/\n/g, '');
            if (javaTrim(strValue) == "") {
                layer.msg('内容不能为空！', { icon: 2, time: 2000 });
                return false;
            }

            var $data = new Object();
            $data.id = $(this).prev().val();
            $data.content = $(this).prev().prev().val();
            $data.person = $(this).prev().prev().prev().val();
            $data.detail_id = $(this).prev().prev().prev().prev().val();
            $data.title = $(this).prev().prev().prev().prev().prev().val();

            $.ajax({
                url: ThinkPHP['AJAX'] + '/Summary/comment',
                data: $data,
                type: 'POST',
                dataType: 'json',
                success: function success(response) {
                    if (response.flag > 0) {
                        layer.msg(response.msg, { time: 2000, icon: 1 });
                    } else {
                        layer.msg(response.msg, { time: 2000, icon: 2 });
                    }
                    setTimeout(function () {
                        location.reload();
                    }, 2000);
                }
            });

            var index = layer.load(2, {
                shade: [0.5, '#fff'] //0.1透明度的白色背景
            });
        });

        // 发布总结
        $('#publish-btn').click(function () {
            var id = $(this).attr('data-id');
            layer.confirm('总结发布后将无法再次编辑，确认发布吗？', {
                icon: 3,
                btn: ['确认', '取消']
            }, function () {
                var loading = layer.load(2, {
                    shade: [0.5, '#fff']
                });
                publishSummary(id).then(function (response) {
                    layer.closeAll();
                    if (response.errno) {
                        layer.msg(response.message, { icon: 2, time: 3000 });
                    } else {
                        layer.msg(response.message, { icon: 1, time: 1500 });
                        setTimeout(function () {
                            location.reload();
                        }, 1500);
                    }
                });
            });
        });

        $('#delete-btn').click(function () {
            var id = $(this).attr('data-id');
            layer.confirm('总结删除后将无法恢复，确认删除吗?', {
                icon: 3,
                btn: ['确认', '取消']
            }, function () {
                var loading = layer.load(2, {
                    shade: [0.5, '#fff']
                });
                deleteSummary(id).then(function (response) {
                    layer.closeAll();
                    if (response.errno) {
                        layer.msg(response.message, { icon: 2, time: 3000 });
                    } else {
                        layer.msg(response.message, { icon: 1, time: 1500 });
                        setTimeout(function () {
                            location.href = '/Summary/draft';
                        }, 1500);
                    }
                });
            });
        });

        function publishSummary(id) {
            return new Promise(function (resolve, reject) {
                $.post('/Summary/publish', {
                    id: id
                }, function (response) {
                    resolve(response);
                }, 'json');
            });
        }

        function deleteSummary(id) {
            return new Promise(function (resolve, reject) {
                $.post('/Summary/delete', {
                    id: id
                }, function (response) {
                    resolve(response);
                }, 'json');
            });
        }
    });
});